package com.qidian.buy.util.data;

import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author 郭浩
 */
public class ThreadStorage {
    @Value("${clear-cache-thread-interval}")
    private static long interval = 60;
    private static LinkedList<Thread> ts = new LinkedList<>();
    public static void push(Thread t){
        ts.push(t);
    }
    static {
        // 开启一个定时任务，在指定的时间间隔中清理线程
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                for (Thread t : ts){
                    if(!t.isAlive()){
                        ts.remove(t);
                    }
                }
            }
        },new Date(),interval * 1000);
    }
}
